package com.wans.guli.member.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.wans.guli.member.feign.FeignCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wans.guli.member.entity.MemberEntity;
import com.wans.guli.member.service.MemberService;
import com.wans.guli.common.utils.PageUtils;
import com.wans.guli.common.utils.R;


/**
 * 会员
 *
 * @author wans
 * @email wansconst@163.com
 * @date 2025-08-01 16:25:57
 */
@RestController
@RequestMapping("member/member")
@Slf4j
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private FeignCouponService feignCouponService;


    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public R test() {
        R test = feignCouponService.test();
        return Objects.requireNonNull(R.ok().put("data-m", "hello world member port 18002")).put("data-c", test.get("data-c"));
    }

    /**
     * 登录
     *
     * @param loginAccount 账号、手机号
     * @param password     密码
     * @return 结果
     */
    @PostMapping("/loginUser")
    R login(@RequestParam("loginAccount") String loginAccount, @RequestParam("password") String password) {
        MemberEntity loginMember = null;
        try {
            loginMember = memberService.login(loginAccount, password);
        } catch (Exception e) {
            log.error("登录异常{}", e.getMessage());
            return R.error(e.getMessage());
        }

        return R.success(loginMember);
    }


    @GetMapping("/getUserInfo")
    @ResponseBody
    public R getUserInfo(@RequestParam("accessToken") String accessToken) {

        return memberService.getUserInfo(accessToken);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public R register(@RequestBody MemberEntity member) {
        try {
            memberService.register(member);
        } catch (Exception e) {
            log.error("注册异常{}", e.getMessage());
            return R.error(e.getMessage());
        }

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
